package com.miraclengine.karma;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import org.andengine.util.debug.Debug;

import com.miraclengine.framework.FileIO;

public class Settings {
	public static boolean musicEnabled = true;
	public static boolean soundEnabled = true;
	
	public final static int[] highscores = new int[] {100, 80, 50, 30, 10};
	
	public final static String file = ".karmasetting";
	
	public static void load(FileIO files) {
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(files.readFile(file)));
			musicEnabled = Boolean.parseBoolean(in.readLine());
			soundEnabled = Boolean.parseBoolean(in.readLine());
			for(int i=0 ; i<highscores.length ; i++) {
				highscores[i] = Integer.parseInt(in.readLine());
			}
			
			Debug.d("TAG", "musicEnabled : " + musicEnabled + ", soundEnabled : " + soundEnabled);
		} catch(IOException e) {
			//:(It's ok we have defaults
			Debug.d("TAG", e);
		} catch(NumberFormatException e) {
			//:(It's ok, defaults save our day
			Debug.d("TAG", e);
		} finally {
			try {
				if(in != null) {
					in.close();
				}
			} catch(IOException e) {
			}
		}
	}
	
	public static void save(FileIO files) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(files.writeFile(file)));
			out.write(Boolean.toString(musicEnabled));
			out.write("\n");
			out.write(Boolean.toString(soundEnabled));
			out.write("\n");
			for(int i=0 ; i<highscores.length ; i++) {
				out.write(Integer.toString(highscores[i]));
				out.write("\n");
			}
		} catch(IOException e) {
			Debug.d("TAG", e);
		} finally {
			try {
				if(out != null) {
					out.close();
				}
			} catch(IOException e) {
			}
		}
	}
}
